/*******************************************************************************
 * Copyright (c) 2005, 2014 springside.github.io
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 *******************************************************************************/
package net.wofly.right.repository;

import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;

import net.wofly.right.domain.Role;
import net.wofly.right.domain.User;
import net.wofly.right.domain.UserRole;


public interface UserRoleRepository extends PagingAndSortingRepository<UserRole, String>, JpaSpecificationExecutor<UserRole> {
    
    List<UserRole> findByUserID(String userID);
    
    List<UserRole> findByRoleID(String roleID);
    
    @Query("select e from User e , UserRole er where e.userID = er.userID and er.roleID = :roleID")
    List<User> findUsersByRoleID(@Param(value = "roleID") String roleID);
    
    @Query("select e from User e , UserRole er where e.userID = er.userID and er.roleID in :roleIDs")
    List<User> findUsersByRoleIDs(@Param(value = "roleIDs") List<String> roleIDs);
    
    @Query("select r from Role r , UserRole er where r.roleID = er.roleID and er.userID = :userID")
    List<Role> findRolesByUserID(@Param(value = "userID") String userID);

    /**
     * 根据角色ID分页查询用户列表
     *
     * @param roleID
     * @param pageable
     * @return
     */
    @Query("select e from User e , UserRole er where e.userID = er.userID and er.roleID = :roleID")
    Page<User> findPageByRoleID(@Param(value = "roleID") String roleID, Pageable pageable);
}